<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input name="id" id="id" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block">
            <input type="text" id="types" name="types" lay-verify="required" lay-reqtext="类型不能为空" placeholder="请输入类型" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">座位数</label>
        <div class="layui-input-block">
            <input type="number" id="sitnum" name="sitnum" lay-verify="required" lay-reqtext="座位数不能为空" placeholder="请输入座位数" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">价格</label>
        <div class="layui-input-block">
            <input type="number" id="price" name="price" lay-verify="required" lay-reqtext="价格不能为空" placeholder="请输入价格" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">数量</label>
        <div class="layui-input-block">
            <input type="number" id="num" name="num" lay-verify="required" lay-reqtext="数量不能为空" placeholder="请输入数量" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">图片地址</label>
        <div class="layui-input-block">
            <input type="text" id="picture" name="picture" lay-verify="required" lay-reqtext="图片地址不能为空" placeholder="请输入图片地址" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="layui/layui.js" charset="utf-8"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
    $(function () {
        var teacherid;
        var classfiyid;
        var id = window.localStorage.getItem("id");
        $.ajax({
            url: "/Car/CarById/" + id,
            dataType: "json",
            type: 'post',
            contentType: 'application/json',
            success: function (res) {
                var data = res
                $("#id").val(data.id);
                $("#name").val(data.name);
                $("#types").val(data.types);
                $("#sitnum").val(data.sitnum);
                $("#price").val(data.price);
                $("#num").val(data.num);
                $("#picture").val(data.picture);
            }
        })
    });

    function findClassAndTeacher(classfiyid, teacherid) {
        $.ajax({
            url: "/Car/Cars",
            dataType: "json",
            success: function (res) {
                console.log(res.data);
                //来去动态赋值给select 选择框
                $.each(res.data, function (index, item) {
                    debugger;
                    if (item.id == classfiyid) {
                        console.log(classfiyid);
                        $("#courseClassfiy").append(new Option(item.courseClassifyName, item.id, false, true))
                    } else {
                        $("#courseClassfiy").append(new Option(item.courseClassifyName, item.id))
                    }

                })
            }
        })

        $.ajax({
            url: "/Car/Cars",
            dataType: "json",
            success: function (res) {
                $.each(res.data, function (index, item) {
                    if (teacherid == item.id) {
                        console.log(classfiyid);
                        $("#teacherSelect").append(new Option(item.teacherName, item.id, false, true))
                    } else {
                        $("#teacherSelect").append(new Option(item.teacherName, item.id))
                    }

                })
            }
        })
    }
</script>
<script>
    layui.use(['form', 'upload', 'element'], function () {
        var form = layui.form,
            upload = layui.upload,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: '/Car/CarUpdate',
                data: JSON.stringify(data.field),
                type: "post",
                contentType: "application/json",
                dataType: "json",
                success: function (res) {
                    if (res.code == 0) {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                }
            })
            return false;
        });
        //重新加载select 标签
        form.render("select");

    });
</script>
</body>
</html>